package cn.com.orderss.controller;

import cn.com.orderss.config.Result;
import cn.com.orderss.entity.Order;
import cn.com.orderss.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.Duration;
import java.time.Instant;
import java.util.List;

@RestController
@RequestMapping("/ry/orders")
@Api(tags = "订单服务")
public class OrderController {

    @Resource
    private OrderService orderService;

    private Instant startTime;

    @PostMapping("/add")
    @ApiOperation(value = "新增订单")
    public Result add(@RequestBody Order order) {
        orderService.add(order);
        return Result.success();
    }

    @GetMapping("/upState")
    @ApiOperation(value = "维修中跳转时间")
    public Result update3() {
        orderService.upState();
        return Result.success();
    }

    @Scheduled(fixedRate = 30000)
    public Result update() {
        if (startTime == null) {
            startTime = Instant.now();
        }
        Duration elapsed = Duration.between(startTime, Instant.now());
        if (elapsed.getSeconds() > 20) {
            orderService.update();
            return Result.success();
        }
        return Result.success();
    }


    @GetMapping("/selectAll")
    @ApiOperation(value = "查询所有")
    public Result selectAll() {
        List<Order> list = orderService.selectAll();
        return Result.success(list);
    }


}
